﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace mamnontuoitho7.AppCode
{
    public class Class
    {

        public ConnectGetData _connectData;
        SqlCommand _cmd;
        String _sql = "";

   
        public Class() {        
            _connectData =  new ConnectGetData();
            _cmd = new SqlCommand(_sql, _connectData.GetConnect());

            //connectData.GetDataSet("");
        }

        /// <summary>
        /// Create New Class for kindergarten
        /// </summary>
        /// <param name="classCategoryID"></param>
        /// <param name="className"></param>
        /// <param name="classPosition"></param>
        /// <returns></returns>
        public bool ClassCreate(String classCategoryID, String className, String classPosition)
        {

            try
            {
                SqlConnection connect = _connectData.GetConnect();
                string sqlUp = "Insert into Class(classCategoryID, ClassName, ClassPosition) values(@classCategoryID, @className, @classPosition)";
                SqlCommand cmd = new SqlCommand(sqlUp, connect);
                cmd.Parameters.AddWithValue("@classCategoryID", classCategoryID);
                cmd.Parameters.AddWithValue("@className", className);
                cmd.Parameters.AddWithValue("@classPosition", classPosition);

                connect.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                connect.Close();

            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii: " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// Get Class list based on class category
        /// </summary>
        /// <param name="classCategoryID"></param>
        /// <returns></returns>
        public DataTable GetClassList(String classCategoryID)
        {
            if(classCategoryID == "0"){
                return _connectData.GetDataTable("select * from Class");
            }else{
                return _connectData.GetDataTable("select * from Class where ClassCategoryID = '"+classCategoryID+"'");
            }
            
        }

        public DataRow GetClassInformation(String classID)
        {
            string sql = "select * from Class where classID = @classID";
            SqlCommand cmd = new SqlCommand(sql, _connectData.GetConnect());
            cmd.Parameters.AddWithValue("@classID", classID);

            return _connectData.GetDataTable(cmd).Rows[0];
        }

        /// <summary>
        /// Upload new information for a class
        /// </summary>
        /// <param name="classID"></param>
        /// <param name="classCategoryID"></param>
        /// <param name="className"></param>
        /// <param name="classPosition"></param>
        /// <returns></returns>
        public bool UploadClass(String classID, String classCategoryID, String className, String classPosition)
        {
            try
            {
                SqlConnection con = _connectData.GetConnect();
                string sqlUp = "update Class set ClassCategoryID = @classCategoryID, " +
                                                   " ClassName = @className, " +
                                                   " ClassPosition = @classPosition " +
                                                   " where ClassID = @classID ";

                SqlCommand cmd = new SqlCommand(sqlUp, con);
                cmd.Parameters.AddWithValue("@ClassCategoryID", classCategoryID);
                cmd.Parameters.AddWithValue("@ClassName", className);
                cmd.Parameters.AddWithValue("@ClassPosition", classPosition);
                cmd.Parameters.AddWithValue("@ClassID", classID);

                con.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            return true;
        }

        /// <summary>
        /// Get Information for shool
        /// </summary>
        /// <returns></returns>
        internal DataRow GetShoolInformation()
        {
            string sql = "select top(1) * from SchoolInformation";
            SqlCommand cmd = new SqlCommand(sql, _connectData.GetConnect());

            return _connectData.GetDataTable(cmd).Rows[0];
        }

        /// <summary>
        /// Save Information of shool
        /// </summary>
        /// <returns></returns>
        internal bool EditShoolInformation(String name, String address, String email, String phone, String fax, String website)
        {
            try
            {
                SqlConnection con = _connectData.GetConnect();
                string sql = "Update SchoolInformation  set " + " Name = @name, " +
                                                                " Address = @address," +
                                                                " Phone = @phone," +
                                                                " Fax = @fax, " +
                                                                " Email = @email, " +
                                                                " Website= @website";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@name", name);
                cmd.Parameters.AddWithValue("@address", address);
                cmd.Parameters.AddWithValue("@phone", phone);
                cmd.Parameters.AddWithValue("@fax", fax);
                cmd.Parameters.AddWithValue("@email", email);
                cmd.Parameters.AddWithValue("@website", website);

                con.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
                return false;
            }
            return true;
        }

        /// <summary>
        /// Get class info by teacher
        /// </summary>
        /// <returns></returns>
        internal DataRow GetClassNameByTeacherID(String teacherID)
        {
            string sql = "select ClassName from TeacherDetailsView where MemberID="+teacherID;
            SqlCommand cmd = new SqlCommand(sql, _connectData.GetConnect());

            return _connectData.GetDataTable(cmd).Rows[0];
        }
    }
}